﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.ViewModel.Report
{
    public class ReportTableBase
    {
        public int Row { get; set; }
        public int Column { get; set; }
        /// <summary>
        /// 画单元格
        /// </summary>
        /// <param name="table"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="content"></param>
        public void DrawCell(Grid table, int row, int column, string content,
            int firstRow,int firstColumn,
            SolidColorBrush background = null,
            SolidColorBrush foreground = null)
        {
            if (row != firstRow || column != firstColumn)
                return;
            var label = new Label();
            label.HorizontalContentAlignment = HorizontalAlignment.Center;
            label.VerticalContentAlignment = VerticalAlignment.Center;
            label.FontSize = 13;
            label.Content = content;

            if (background != null)
                label.Background = background;
            if (foreground != null)
                label.Foreground = foreground;

            var border = new Border();
            border.BorderThickness = new Thickness(1, 1, 0, 0);
            border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));

            Grid.SetRow(border, row);
            Grid.SetColumn(border, column);

            Grid.SetRow(label, row);
            Grid.SetColumn(label, column);

            table.Children.Add(label);
            table.Children.Add(border);
        }
        /// <summary>
        /// 画合并单元格（rowspan）
        /// </summary>
        /// <param name="table"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="content"></param>
        /// <param name="rowSpan"></param>
        /// <param name="firstRow"></param>
        public void DrawRowSpanCell(Grid table, int row, int column, string content, 
            int rowSpan, int firstRow,int firstColumn,
            SolidColorBrush background = null,
            SolidColorBrush foreground = null)
        {
            if (row >= firstRow + rowSpan || row < firstRow)
                return;

            if (column != firstColumn)
                return;
            var label = new Label();
            label.HorizontalContentAlignment = HorizontalAlignment.Center;
            label.VerticalContentAlignment = VerticalAlignment.Center;
            label.FontSize = 13;
            label.Content = content;

            if (background != null)
                label.Background = background;
            if (foreground != null)
                label.Foreground = foreground;

            var border = new Border();
            border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));

            Grid.SetRow(border, row);
            Grid.SetColumn(border, column);

            Grid.SetRow(label, row);
            Grid.SetColumn(label, column);

            if (row == firstRow)
            {
                border.BorderThickness = new Thickness(1, 1, 0, 0);
                Grid.SetRowSpan(label, rowSpan);
                table.Children.Add(label);
                table.Children.Add(border);
            }
            else
            {
                border.BorderThickness = new Thickness(1, 0, 0, 0);
                table.Children.Add(border);
            }






        }
        /// <summary>
        /// 画合并单元格（columnspan）
        /// </summary>
        /// <param name="table"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="content"></param>
        /// <param name="columnSpan"></param>
        /// <param name="firstColumn"></param>
        public void DrawColumnSpanCell(Grid table, int row, int column,
            string content, int columnSpan, int firstRow, int firstColumn,
            SolidColorBrush background = null,
            SolidColorBrush foreground = null)
        {
            if (row != firstRow)
                return;
            if (column >= firstColumn + columnSpan || column < firstColumn)
                return;
            var label = new Label();
            label.HorizontalContentAlignment = HorizontalAlignment.Center;
            label.VerticalContentAlignment = VerticalAlignment.Center;
            label.FontSize = 13;
            label.Content = content;

            if (background != null)
                label.Background = background;
            if (foreground != null)
                label.Foreground = foreground;

            var border = new Border();
            border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));

            Grid.SetRow(border, row);
            Grid.SetColumn(border, column);

            Grid.SetRow(label, row);
            Grid.SetColumn(label, column);

            if (column == firstColumn)
            {
                border.BorderThickness = new Thickness(1, 1, 0, 0);
                Grid.SetColumnSpan(label, columnSpan);
                table.Children.Add(label);
                table.Children.Add(border);
            }
            else
            {
                border.BorderThickness = new Thickness(0, 1, 0, 0);
                table.Children.Add(border);
            }


        }

        /// <summary>
        /// 画边框
        /// </summary>
        /// <param name="table"></param>
        public void DrawBorder(Grid table)
        {
            for (int i = 0; i < Row; i++)
            {
                var border = new Border();
                border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));
                border.BorderThickness = new Thickness(0, 0, 1, 0);
                Grid.SetRow(border, i);
                Grid.SetColumn(border, Column - 1);
                table.Children.Add(border);
            }
            for (int i = 0; i < Column; i++)
            {
                var border = new Border();
                border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));
                border.BorderThickness = new Thickness(0, 0, 0, 1);
                Grid.SetRow(border, Row - 1);
                Grid.SetColumn(border, i);
                table.Children.Add(border);
            }
        }
    }
}
